package com.janal.model.dao.user;

import com.janal.model.dao.Basedao.Basedao;
import com.janal.util.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{
    @Override
    public User getLoginUser(Connection conn, String username) throws Exception {
        User resultuser = null;
        ResultSet rs = null;
        PreparedStatement pstm = null;

        if(conn != null){
            String sql = "select * from user where username = ?";
            Object[] params = {username};
            rs = Basedao.execute(conn,sql,params,rs,pstm);
            if(rs.next()){
                resultuser = new User();
                resultuser.setUsername(rs.getString("username"));
                resultuser.setPassword(rs.getString("password"));
            }
            utils.close(rs,pstm,null);
        }
        return resultuser;
    }

    @Override
    public boolean setRegisterUser(Connection conn, User user) throws Exception {
        PreparedStatement pstm = null;
        if(conn != null){
            String sql = "insert into user values(?,?)";
            Object[] params = {user.getUsername(),user.getPassword()};
            int updateRows = Basedao.execute(conn,sql,params,pstm);
            utils.close(pstm,null);
            if(updateRows > 0){
                return true;
            }
        }
        return false;
    }
}
